/**
 * Property & Copyright 2009 Nokia Siemens Networks.
 */
package beans.grid.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import play.Logger;

import beans.grid.midas.CommandClassGrid;
import beans.grid.midas.CommandGroupGrid;
import beans.grid.midas.ProgramDataGrid;


public abstract class Grid{
	public static final String GRID_SCRIPT_TEMPLATE_PATH = "grid/grid.js";
	public static final String GRID_COMMON_TEMPLATE_PATH = "grid/grid.html";
	
	public static final String COMMAND_CLASS_PATH = "commandClass";
	public static final String COMMAND_GROUP_PATH = "commandGroup";
	public static final String PROGRAM_DATA_PATH = "programData";
	
	private static Map<String,Class<? extends Grid>> PATH_CLAZZ_MAP = new HashMap<String,Class<? extends Grid>>(); 
	static{
		PATH_CLAZZ_MAP.put(COMMAND_CLASS_PATH,CommandClassGrid.class);
		PATH_CLAZZ_MAP.put(COMMAND_GROUP_PATH,CommandGroupGrid.class);
		PATH_CLAZZ_MAP.put(PROGRAM_DATA_PATH,ProgramDataGrid.class);
	}
	
	protected List<GridColumn> columns;
	protected int page = 1;
	protected int pageSize = 10;
	protected String sortName;
	protected String sortOrder;
	protected boolean scroll = true;
	protected String title;
	
	protected String dataPath;
	protected String scriptPath;
	protected String elementId;
	protected String pagerId;
	protected String dbClickRowCallbackFunctionName = "dbclickrowCallback";
	
	
	public Grid(String dataPath,String elementId,String pagerId,String title,String scriptPath){
		this.dataPath = dataPath;
		this.scriptPath = scriptPath;
		this.elementId = elementId;
		this.pagerId = pagerId;
		this.title = title;
		constructGrid();
	}
	
	public static Grid getInstance(String clazz){
		try {
			return PATH_CLAZZ_MAP.get(clazz).newInstance();
		} catch (Exception e) {
			Logger.error(e.getMessage(), e);
		}
		return null;
	}
	
	public abstract void constructGrid();
	
	public void addColumn(GridColumn col){
		if(columns == null){
			columns = new ArrayList<GridColumn>();
		}
		columns.add(col);
	}
	
	public List<GridColumn> getColumns() {
		return columns;
	}
	public void setColumns(List<GridColumn> columns) {
		this.columns = columns;
	}
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getSortName() {
		return sortName;
	}

	public void setSortName(String sortName) {
		this.sortName = sortName;
	}

	public String getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}

	public boolean isScroll() {
		return scroll;
	}
	
	public void setScroll(boolean scroll) {
		this.scroll = scroll;
	}

	public String getTitle() {
		return title;
	}

	/*return column names in format of A,B,C*/
	public String getColumnNames(){
		if(columns == null || columns.size() == 0){
			return "";
		}
		List<String> list = new ArrayList<String>();
		for(GridColumn col : columns){
			list.add("'" + col.getName() + "'");
		}
		return StringUtils.join(list, ',');
	}

	public String getRowList(){
		return "[10,20,50]";
	}
	
	public abstract String getGridIdentifer();

	public String getDataPath() {
		return dataPath;
	}

	public String getElementId() {
		return elementId;
	}

	public String getPagerId() {
		return pagerId;
	}

	public String getDbClickRowCallbackFunctionName() {
		return dbClickRowCallbackFunctionName;
	}

	public void setDbClickRowCallbackFunctionName(
			String dbClickRowCallbackFunctionName) {
		this.dbClickRowCallbackFunctionName = dbClickRowCallbackFunctionName;
	}
	
	public String getScriptPath() {
		return scriptPath;
	}
}
